package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.service.impl.IAttrecordServiceImpl;
import com.cn.jdia.tas.utils.ConstantsUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class SignController {

	@Resource
	private IAttrecordServiceImpl attService = null;

	@RequestMapping("/sign")
	public String signUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String data = request.getParameter("data");
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String name = map.get("name");
		String id = map.get("id");
		String position = map.get("position");

		System.out.println(id + name + position);

		// 1 山东省青岛市城阳区宝安路57
		// 2 山东省青岛市城阳区民航路99
		// 3 山东省青岛市城阳区民航路
		// 4 山东省青岛市城阳区民航路
		//
		// 6胶东机场建设指挥部
		// 7 ITC大楼

		int ret = -1;
		for (int i = 0; i < 5; i++) {
			ret = insertRecord(id, position, "");
			if (ret > 0) {
				break;
			}
		}

		out.write(ret + "");
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/outsign")
	public String outSignUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String data = request.getParameter("data");
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String name = map.get("name");
		String id = map.get("id");
		String position = map.get("position");
		String loc = map.get("loc");

		System.out.println(id + name + position);

		int ret = -1;
		for (int i = 0; i < 5; i++) {
			ret = insertRecord(id, position, loc);
			if (ret > 0) {
				break;
			}
		}

		out.write(ret + "");
		out.flush();
		out.close();

		return null;
	}

	public int insertRecord(String id, String positon, String subdescription) {
		int maxRecordId = attService.getMaxRecordId();
		System.out.println(maxRecordId);

		DateTime datetime = new DateTime();
		String checkdate = datetime.toString("YYYY-MM-dd");
		String checktime = datetime.toString("HH:mm");
		String ctime = datetime.toString("YYYY-MM-dd HH:mm:ss");

		String description = "";
		if ("1".equals(positon)) {
			description = ConstantsUtils.POSITION_1;
		} else if ("2".equals(positon)) {
			description = ConstantsUtils.POSITION_2;
		} else if ("3".equals(positon)) {
			description = ConstantsUtils.POSITION_3;
		} else if ("4".equals(positon)) {
			description = ConstantsUtils.POSITION_4;
		} else if ("5".equals(positon)) {
			description = ConstantsUtils.POSITION_5;
		} else if ("6".equals(positon)) {
			description = ConstantsUtils.POSITION_6;
		} else if ("7".equals(positon)) {
			description = ConstantsUtils.POSITION_7;
		} else if ("8".equals(positon)) {
			description = ConstantsUtils.POSITION_8;
		} else if ("9".equals(positon)) {
			description = ConstantsUtils.POSITION_9;
		} else if ("-1".equals(positon)) {
			description = subdescription;
		}

		T_attrecord record = new T_attrecord();
		record.setAttrecordid(++maxRecordId);
		record.setAttmachineid(0);
		record.setEmployeeid(id);
		record.setCheckdate(checkdate);
		record.setChecktime(checktime);

		record.setChecktype("地理签到");

		record.setDescription(positon);
		record.setCreatetime(ctime);
		record.setUpdatetime(ctime);
		record.setAttmachinename(description);

		int inret = attService.addSelective(record);
		System.out.println(inret);

		return inret;
	}

}
